﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

//add the following references
using System.Collections.ObjectModel; //for Collection<t>
using System.Net; //for HttpWebRequest,HttpWebResponse
using System.IO; //for stream

namespace PushNotification
{
  public class PushNotificationService : IPushNotificationService
  {
    //add the static collection to hold the subscribed URIs
    static Collection<DeviceRef> DeviceRefCollection = new Collection<DeviceRef>();

    //implementation of the Subscribe method
    //which accept channelURI as a parameter, 
    //in this example called from Window Mobile 7 application
    public void Subscribe(int deviceID, string channelURI)
    {
      DeviceRefCollection.Add(new DeviceRef(deviceID, channelURI));
    }

    //implementation of the SendNotification method
    //which accept message as a parameter, 
    //in this example called from WPF application
    public string SendNotification(int deviceID, string message)
    {
      string notificationStatus = "";
      string notificationChannelStatus = "";
      string deviceConnectionStatus = "";

      //loop through deviceref to get the ChannelURI and send toast 
      //for each device/windows phone app subscribed.

      foreach (DeviceRef item in DeviceRefCollection)
      {
        if (deviceID == item.DeviceID)
        {
          //create the HttpWebRequest to Microsoft Notification Server
          //this require internet connection
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create
              (item.ChannelURI);
          request.Method = "POST";
          //request.ContentType = "text/xml; charset=utf-8";
          request.Headers["X-MessageID"] = Guid.NewGuid().ToString();
          request.Headers.Add("X-NotificationClass", "3");
          //request.Headers.Add("X-WindowsPhone-Target", "toast");

          byte[] contents = Encoding.ASCII.GetBytes(message); ;

          request.ContentLength = contents.Length;
          //write to request stream
          using (Stream requestStream = request.GetRequestStream())
          {
            requestStream.Write(contents, 0, contents.Length);
          }

          //get the HttpWebResponse status from Microsoft Notification Server
          using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
          {
            notificationStatus = response.Headers["X-NotificationStatus"];
            notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
            deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
          }
        }
      }
      //return the status to WPF Application
      return notificationStatus + " : " + notificationChannelStatus +
          " : " + deviceConnectionStatus;
    }
  }

  public class DeviceRef
  {
    public DeviceRef(int deviceID, string channelURI)
    {
      DeviceID = deviceID;
      ChannelURI = channelURI;
    }
    public string ChannelURI { get; set; }
    public int DeviceID { get; set; }
  }
}
